layui.config({
  version: '1593348928630' //为了更新 js 缓存，可忽略
});

layui.use(['layer', 'table', 'element', 'form'], function () {
  var $ = layui.$
    , layer = layui.layer //弹层
    , table = layui.table //表格
    , element = layui.element
    , form = layui.form
    , saveUrls = jsRoutes.controllers.admin.WiFiController.save()

  layer.msg('Hello World');
  //执行一个 table 实例
  var demotable = table.render({
    elem: '#demotable'
    , contentType: 'application/json'
    , method: saveUrls.method
    , height: 550
    , url: saveUrls.url //数据接口
    , title: 'WiFi表'
    , page: true //开启分页
    , toolbar: 'default' //开启工具栏，此处显示默认图标，可以自定义模板，详见文档
    , where: { "op": "list" }
    , cols: [[ //表头
      { type: 'checkbox', fixed: 'left' }
      , { field: 'date', title: '日期', width: 110, fixed: 'left' }
      , { field: 'online', title: '公开', width: 80 }
      , { field: 'name', title: 'SSID', width: 180 }
      , { field: 'password', title: '密码', width: 200 }
      , { field: 'note', title: '备注' }

    ]]
  });



  //监听头工具栏事件
  table.on('toolbar(test)', function (obj) {
    var checkStatus = table.checkStatus(obj.config.id)
      , data = checkStatus.data; //获取选中的数据
    switch (obj.event) {
      case 'add':
        //    layer.msg('添加');
        layer.open({
          type: 1
          , area: ['420px', '330px']
          , content: $('#edit-form') //这里content是一个普通的String
          , btn: ['提交', '关闭']
          , yes: function (index, layero) {
            //按钮【按钮一】的回调
            console.log(index)
            var data = form.val("layer-edit-form");
            var load_index = layer.load(1, { time: 30 * 1000 });
            $.post({
              url: saveUrls.url,
              contentType: 'application/json; charset=UTF-8',
              data: JSON.stringify({ "op": "create", "name": data.name, "password": data.password, "note": data.note, "online": data.online }),
              //                 dataType: 'text',
              success: function (data) {
                console.log(data);
                layer.close(load_index);
                layer.msg("数据: \n" + data + "\n状态: " + status);
                demotable.reload();
              }
            });
            layer.close(index);
          }
          , btn2: function (index, layero) {
            //按钮【按钮二】的回调
            //return false 开启该代码可禁止点击该按钮关闭
          }
          , cancel: function () {
            //右上角关闭回调
            //return false 开启该代码可禁止点击该按钮关闭
          }

        });
        break;
      case 'update':
        if (data.length === 0) {
          layer.msg('请选择一行');
        } else if (data.length > 1) {
          layer.msg('只能同时编辑一个');
        } else {
          console.log(checkStatus.data)
          //layer.alert('编辑 [id]：'+ checkStatus.data[0].name);
          form.val("layer-edit-form", {
            "name": checkStatus.data[0].name,
            "password": checkStatus.data[0].password,
            "note": checkStatus.data[0].note,
            "online": checkStatus.data[0].online == '是'
          })
          checkStatus.data[0].online == '是'
          document.getElementById("layer-edit-form-name").readOnly = true;
          document.getElementById("layer-edit-form-name").style = 'background-color:#eee;';
          layer.open({
            type: 1
            , area: ['420px', '330px']
            , content: $('#edit-form')  //这里content是一个普通的String
            , btn: ['提交', '关闭']
            , yes: function (index, layero) {
              //按钮【按钮一】的回调
              console.log(index);
              var data = form.val("layer-edit-form");
              var load_index = layer.load(1, { time: 30 * 1000 });
              $.post({
                url: saveUrls.url,
                contentType: 'application/json; charset=UTF-8',
                data: JSON.stringify({ "op": "update", "name": data.name, "password": data.password, "note": data.note, "online": data.online }),
                //                    dataType: 'text',
                success: function (data) {
                  console.log(data);
                  layer.close(load_index);
                  layer.msg("数据: \n" + data + "\n状态: " + status);
                  demotable.reload();
                }
              });
              layer.close(index);

            }
            , btn2: function (index, layero) {
              //按钮【按钮二】的回调

              //return false 开启该代码可禁止点击该按钮关闭
            }
            , cancel: function () {
              //右上角关闭回调

              //return false 开启该代码可禁止点击该按钮关闭
            }

          });
        }
        break;
      case 'delete':
        if (data.length === 0) {
          layer.msg('请选择一行');
        } else {
          layer.msg('删除');
          console.log(data)
          layer.open({
            type: 1,
            content: "确定要删除SSID：<code>" + checkStatus.data[0].name + "<code>？"  //这里content是一个普通的String
            , btn: ['提交', '关闭']

            , yes: function (index, layero) {
              //按钮【按钮一】的回调
              console.log(index);
              var data = layero.context.forms[0];

              var load_index = layer.load(1, { time: 30 * 1000 });

              $.post({
                url: saveUrls.url,
                contentType: 'application/json; charset=UTF-8',
                data: JSON.stringify({ "op": "delete", "name": checkStatus.data[0].name }),
                //                    dataType: 'text',
                success: function (data) {
                  console.log(data);
                  layer.close(load_index);
                  layer.msg("数据: \n" + data + "\n状态: " + status);
                  demotable.reload();
                }
              });
              layer.close(index);

            }
            , btn2: function (index, layero) {
              //按钮【按钮二】的回调

              //return false 开启该代码可禁止点击该按钮关闭
            }
            , cancel: function () {
              //右上角关闭回调

              //return false 开启该代码可禁止点击该按钮关闭
            }
          });


        }
        break;
    };
  });

});

